package com.example.xml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

public class SubTest {

	public static void main(String[] args) throws UnsupportedEncodingException {
		String path = XmlUtilTest.class.getResource("").getPath() + "/DZKH811420210813195822877";
		String txt = readTxtFile(path, "GBK");
		System.out.println(txt.getBytes("GBK").length);
		String txtUTF8 = new String(txt.getBytes("UTF-8"), "UTF-8");
		System.out.println(txtUTF8.getBytes().length);
		System.out.println(txtUTF8);
	}

	public static String readTxtFile(String filePath, String charset) {
		String result = "";
		try {
			String encoding = charset;
			File file = new File(filePath);
			if (file.isFile() && file.exists()) {
				InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
				BufferedReader bufferedReader = new BufferedReader(read);
				String lineTxt = null;
				while ((lineTxt = bufferedReader.readLine()) != null) {
					result += lineTxt;
				}
				read.close();
			} else {
				System.out.println("找不到指定的文件");
			}
		} catch (Exception e) {
			System.out.println("读取文件内容出错");
			e.printStackTrace();
		}
		return result;
	}
}
